﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;

namespace CodePlex.Resourceful
{
    public static class ResourcefulCoreExtensions
    {

        // broken (or fixed depending on your point of view) in silverlight2 beta 2
        //public static Stream GetRequestStream(this HttpWebRequest target)
        //{
        //    return Utils.MakeSynchronous<Stream>(target.BeginGetRequestStream, target.EndGetRequestStream);
        //}


        public static bool Exists<T>(this List<T> target, Predicate<T> match)
        {
            foreach (T t in target)
            {
                if (match(t))
                {
                    return true;
                }
            }

            return false;
        }


        public static List<TOutput> ConvertAll<TInput,TOutput>(this List<TInput> target, Converter<TInput, TOutput> converter)
        {
            List<TOutput> rt = new List<TOutput>(target.Count);
            foreach (TInput t in target)
            {
                rt.Add(converter(t));
            }
            return rt;
        }
        public static List<T> FindAll<T>(this List<T> target, Predicate<T> match)
        {
            List<T> rt = new List<T>();
            foreach (T t in target)
            {
                if (match(t))
                {
                    rt.Add(t);
                }
            }
            return rt;

        }
        public static T Find<T>(this List<T> target, Predicate<T> match)
        {
            foreach (T t in target)
            {
                if (match(t))
                {
                    return t;
                }
            }
            return default(T);
        }

        public static void ForEach<T>(this IEnumerable<T> target, Action<T> action)
        {
            foreach (T t in target)
            {
                action(t);
            }
        }

    }
}
